package com.psc.presenter.utils;

import com.kpjy.db.gen.HomeModeBeanDao;
import com.kpjy.db.gen.SecondLevelModelBeanDao;
import com.psc.model.a.HomeModeBean;
import com.psc.model.a.SecondLevelListModelBean;
import com.psc.model.a.SecondLevelModelBean;
import com.psc.view.main.ProxyApplication;
import com.psc.view.utils.log.LogUtil;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Creator:Yx
 * Time:2020/7/29 10:34
 * Description:GreenDao 增删改查方法
 */
public class GreenDaoUtils {
    private GreenDaoUtils() {
    }

    private static GreenDaoUtils greenDaoUtils;

    public static GreenDaoUtils getInstance() {
        if (greenDaoUtils == null) {
            synchronized (GreenDaoUtils.class) {
                if (greenDaoUtils == null) {
                    greenDaoUtils = new GreenDaoUtils();
                }
            }
        }
        return greenDaoUtils;
    }


    //======首页保存Start======

    /**
     * 添加数据至数据库,插入实体类，这个是直接插入。
     * insert插入一条数据，主键ID+1个
     */
    public void insertData(HomeModeBean bean) {
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().insert(bean);
    }

    /**
     * 将数据实体 List<>型参数 通过事务添加至数据库
     * insertInTx插入多条数据
     *
     * @param list 集合类型
     */
    public void insertData(List<HomeModeBean> list) {
        if (null == list || list.size() <= 0) {
            return;
        }
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().insertInTx(list);
    }

    /**
     * 添加数据至数据库，
     * save会根据HashKey 这个key就是我新增的Long id这个字段 也就是本地表中的主键
     * 如果存在就update(entity);不存在就insert(entity)；
     *
     * @param bean
     */
    public void saveData(HomeModeBean bean) {
        deleteAllData();
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().save(bean);
        LogUtil.iYx("数据本地保存成功" + bean.getId());
    }

    /**
     * insertOrReplace插入或替换,如果不存在就插入,存在就替换.实际效果和svae很类似，将实体插入到与具体DAO关联的表中
     * insertOrReplaceInTx插入或替换多条数据
     *
     * @param bean
     */
    public void savaInsertOrReplace(HomeModeBean bean) {
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().insertOrReplace(bean);
    }

    /**
     * 删除一条数据，需要明确对应参数与数据库数据都对上 mBeanDao.delete(new Bean(100l, date, 12+"",10));
     *
     * @param bean 删除具体内容
     */
    public void deleteData(HomeModeBean bean) {
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().delete(bean);
        LogUtil.iYx("删除一个对象成功");
    }

    /**
     * 删除一条数据,根据Key 也就是id
     *
     * @param id 删除具体内容
     */
    public void deleteByKeyData(long id) {
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().deleteByKey(id);
    }

    /**
     * deleteAll删除表内数据，删除HomeModeBean该表所有数据
     * deleteInTx删除一组数据(对象)
     * deleteByKeyInTx删除一组数据(根据id)
     */
    public void deleteAllData() {
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().deleteAll();
        LogUtil.iYx("删除该表全部数据成功");
    }

    /**
     * 更新数据库
     * update更新一条数据，是根据自增长ID修改
     * updateInTx 更新一组数据,没有对应id的不做处理
     * ArrayList<Bean> beans = new ArrayList<>();
     * mBeanDao.updateInTx(beans);
     *
     * @param bean
     */
    public void updateData(HomeModeBean bean) {
        DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao().update(bean);
    }

    /**
     * 查询本实体数据
     *
     * @return
     */
    public HomeModeBean query() {
        HomeModeBean bean = DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getHomeModeBeanDao()
                .queryBuilder().build().unique();
        LogUtil.iYx("数据本地查询成功" + (bean != null ? bean.getId() : "-无-"));
        return bean;
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    public List<HomeModeBean> queryAll() {
        QueryBuilder<HomeModeBean> builder = DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession()
                .getHomeModeBeanDao().queryBuilder();
        return builder.build().list();
    }

    /**
     * 查询集合
     *
     * @return
     */
    public List<HomeModeBean> queryList() {
        Query<HomeModeBean> query = DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession()
                .getHomeModeBeanDao().queryBuilder()
                .where(HomeModeBeanDao.Properties.Id.eq(
                        0)).build();
        List<HomeModeBean> entityBuildingPicInfo = query.list();
        return entityBuildingPicInfo;
    }


    //======首页保存End↑======

    //======二级页导航Start↓======

    /**
     * 根据 唯一标识 栏目名 修改栏目首屏导航缓存数据
     *
     * @param navigationNo 栏目名（唯一标识）
     * @param mData        此刻请求到的导航新数据
     */
    public void updateSecondLevelDataNv(String navigationNo, SecondLevelModelBean mData) {
        mData.setOnlyNo(navigationNo);
        SecondLevelModelBean bean = DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao()
                .queryBuilder().where(SecondLevelModelBeanDao.Properties.OnlyNo.eq(navigationNo)).build().unique();
        if (bean != null) {
            LogUtil.iYx("查到有历史导航：" + bean.getOnlyNo());
            mData.setId(bean.getId());
        }
        try {
            //插入或替换
            DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao().insertOrReplace(mData);
            //插入或更新
            //DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao().save(mData);
            LogUtil.iYx("更新导航成功");
        } catch (Exception e) {
            LogUtil.iYx(e.getMessage() + "  " + e.toString());
        }
    }

    /**
     * 更新某栏目下第一个导航对应的数据列表
     *
     * @param typeId 有导航的为：导航ID，用于唯一主键标识，无导航的则为：栏目名
     * @param mList  数据列表
     */
    public void updateSecondLevelDataList(String typeId, List<SecondLevelListModelBean> mList) {
        SecondLevelModelBean modelBean = new SecondLevelModelBean();
        modelBean.setOnlyNo(typeId);
        modelBean.setList(mList);
        SecondLevelModelBean bean = DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao()
                .queryBuilder().where(SecondLevelModelBeanDao.Properties.OnlyNo.eq(typeId)).build().unique();
        if (bean != null) {
            LogUtil.iYx("查到有默认列表数据：" + bean.getOnlyNo());
            modelBean.setId(bean.getId());
        }
        try {
            //插入或替换
            DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao().insertOrReplace(modelBean);
            //插入或修改
            //DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao().save(modelBean);
            LogUtil.iYx("更新默认数据成功");
        } catch (Exception e) {
            LogUtil.iYx(e.getMessage() + "  " + e.toString());
        }
    }

    /**
     * 根据唯一标识，
     * 查询导航
     * 或查询导航下对应的数据列表
     *
     * @param mOnlyNo 唯一标识
     */
    public SecondLevelModelBean getSecondLevelData(String mOnlyNo) {
        SecondLevelModelBean bean = DbManager.getInstance(ProxyApplication.getInstance()).getDaoSession().getSecondLevelModelBeanDao()
                .queryBuilder().where(SecondLevelModelBeanDao.Properties.OnlyNo.eq(mOnlyNo)).build().unique();
        return bean;
    }

    //======二级页导航End↑======

}
